<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>添加用户（POST JSON）</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }
        form div {
            margin-bottom: 10px;
        }
        label {
            display: block;
            margin-bottom: 5px;
        }
        input, textarea {
            width: 100%;
            padding: 8px;
            box-sizing: border-box;
        }
        button {
            padding: 10px 15px;
            background-color: #4CAF50;
            color: white;
            border: none;
            cursor: pointer;
            margin-top: 10px;
        }
        button:hover {
            background-color: #45a049;
        }
    </style>
</head>
<body>
<h1>添加用户信息</h1>
<form id="userForm">
    <div>
        <label for="username">用户名：</label>
        <input type="text" id="username" name="username" required/>
    </div>
    <div>
        <label for="password">密码：</label>
        <input type="password" id="password" name="pwd" required minlength="6"/>
    </div>
    <div>
        <label for="email">邮箱：</label>
        <input type="email" id="email" name="email" required/>
    </div>
    <div>
        <label for="phone">电话：</label>
        <input type="tel" id="phone" name="phone" required/>
    </div>
    <div>
        <label for="qq">QQ号：</label>
        <input type="text" id="qq" name="qq" placeholder="可选，QQ号"/>
    </div>
    <div>
        <label for="info">个人简介：</label>
        <textarea id="info" name="info" rows="4" placeholder="简短介绍自己"></textarea>
    </div>
    <button type="button" onclick="submitUser()">提交</button>
</form>

<script>
    async function submitUser() {
        const form = document.getElementById('userForm');
        const formData = new FormData(form);
        const userData = Object.fromEntries(formData.entries());
        const jsonData = JSON.stringify(userData);

        try {
            const response = await fetch("/user/addUser", {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: jsonData
            });

            const responseText = await response.text();
            if (response.ok) {
                alert('用户添加成功');
            } else {
                alert('用户添加出现问题，请稍后再试');
            }
        } catch (error) {
            console.error('Error:', error);
            alert('网络错误，请稍后再试');
        }
    }
</script>
</body>
</html>